package com.gateway.pay.channel.strategy.factory;


import com.gateway.pay.channel.strategy.IThirdPartyStrategy;
import com.gateway.pay.channel.utils.SpringApplicationContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

/**
 * 为了避免策略
 */
@Component
@Slf4j
public class StrategyThirdPartyFactory implements IThirdPartyFactory {
    @Override
    public IThirdPartyStrategy createStrategy(Object strategyEnum) {
        IThirdPartyStrategy strategy = null;
        String requestStrategyName = null;
        try {
            if (strategyEnum instanceof Object) {
                requestStrategyName = null;//((PlatformTypeEnums) strategyEnum).getRequest();
                if (StringUtils.isBlank(requestStrategyName)) {
                    throw new RuntimeException("未配置此【" + requestStrategyName + "】策略");
                }

                String beanName = requestStrategyName.toLowerCase() + "ThirdStrategy";
                strategy = SpringApplicationContext.getBean(beanName);
            } else if (strategyEnum != null) {
                // Class<?> clazz = Class.forName(strategyName.get);
                strategy = (IThirdPartyStrategy) strategyEnum.getClass().newInstance();
            } else {
                throw new RuntimeException("未配置");
            }
        } catch (Exception e) {
            log.error("策略工厂获取策略失败：", e);
        }
        return strategy;
    }
}
